<template>
  <div v-loading="loading" ref="echart" class="charts"></div>
</template>

<script>
import * as echarts from 'echarts'
export default {
  props: {
    options: {
      type: Object,
      required: true
    },
    loading: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      chart: null
    }
  },
  watch: {
    options: {
      async handler(newVal) {
        this.chart.setOption(this.options, true)
      },
      deep: true
    }
  },
  mounted() {
    this.init()
  },
  methods: {
    async init() {
      this.chart = echarts.init(this.$refs.echart)
      this.chart.setOption(this.options)
      window.addEventListener('resize', () => {
        this.chart.resize() // 窗口发生改变就更新echarts
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.charts {
  width: 100%;
  height: 100%;
}
</style>
